package git.soulbgm.controller;

import git.soulbgm.pojo.ResultData;
import git.soulbgm.pojo.User;
import git.soulbgm.utils.JsonUtil;
import nl.bitwalker.useragentutils.UserAgent;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author 贺瑞杰
 * @version V1.0
 * @date 2019/5/4 0004 下午 20:24
 * @description
 */
@RestController
public class UserController {

    private String username = "hrj";
    private String password = "123";

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @RequestMapping("login")
    public ResultData login(HttpServletRequest request, User user) {
        ResultData rd = new ResultData();
        String token = "";
        // 判断用户名密码正确
        if (this.username.equals(user.getUsername()) && this.password.equals(user.getPassword())) {
            // 获得设备
            String userAgent = request.getHeader("user-agent");
            // 生成token
            token = generateToken(userAgent, username);
            // 将token存入redis中
            redisTemplate.opsForValue().set(token, JsonUtil.toJson(user), 30 * 60, TimeUnit.MINUTES);
            rd.setCode(0);
            rd.setMsg("登录成功");
            rd.setData(token);
        } else {
            rd.setCode(1);
            rd.setMsg("登录失败");
        }
        return rd;
    }

    @RequestMapping("authentication")
    public ResultData authentication(HttpServletRequest request) {
        ResultData rd = new ResultData();
        // 尝试获取token
        String token = request.getHeader("accessToken");
        if (token != null && !"".equals(token)) {
            String redisToken = redisTemplate.opsForValue().get(token);
            if (redisToken != null) {
                // 认证成功
                rd.setMsg("认证成功");
                rd.setCode(0);
                return rd;
            }
        }
        rd.setCode(1);
        rd.setMsg("认证失败");
        return rd;
    }

    @RequestMapping("output")
    public String output() {
        return "output";
    }

    /**
     * 生成token(格式为token:设备-加密的用户名-时间-六位随机数)
     * @param userAgent
     * @param username
     * @return
     */
    public String generateToken(String userAgent, String username){
        StringBuilder token = new StringBuilder();
        //加token:
        token.append("token:");
        //加设备
        UserAgent userAgent1 = UserAgent.parseUserAgentString(userAgent);
        if (userAgent1.getOperatingSystem().isMobileDevice()){
            token.append("MOBILE-");
        } else {
            token.append("PC-");
        }
        //加加密的用户名
        token.append(DigestUtils.md5Hex(username) + "-");
        //加时间
        token.append(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + "-");
        //加六位随机数111111-999999
        token.append(new Random().nextInt((999999 - 111111 + 1)) + 111111);
        System.out.println("token=>" + token.toString());
        return token.toString();
    }

}
